<?php


namespace App\Http\Controllers\Api;

use App\Businesses\BusinessState;
use App\Http\Requests\ApiV2\PlanRequest;
use App\Services\Business\Models\Plan;
use App\Services\Business\Models\UserConquer;
use App\Services\Business\Models\WeightFragment;
use App\Services\Member\Interfaces\UserServiceInterface;
use App\Services\Member\Models\Activity;
use App\Services\Member\Models\User;
use App\Services\Member\Services\UserService;
use Illuminate\Http\JsonResponse;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\DB;
use Illuminate\Support\Facades\Storage;
use Carbon\Carbon;
use App\Services\Business\Models\PlanFragment;


class planController extends BaseController
{


    /**
     * @var UserService
     */
    protected $userService;

    protected $businessState;


    public function __construct(

        UserServiceInterface $userService,
        BusinessState        $businessState
    )
    {

        $this->userService = $userService;
        $this->businessState = $businessState;

    }


    /**
     * @param Request $request
     * @return JsonResponse
     */
    public function index(Request $request)
    {


        $size = $request->input('size');
        $plan = Plan::query()
            ->with(['user:id,nick_name,avatar'])
            ->orderBy('created_at', 'desc')->paginate($size);
        return $this->successData(compact('plan'));
    }


    /**
     * 活动未开始
     * @param Request $request
     * @param $id
     * @return JsonResponse
     */
    public function detail(Request $request, $id)
    {


        $plan = Plan::query()->with(['user'])->where('id', $id)->firstOrFail();
        $isFollowed = false;
        $isThumbsuped = false;
        if ($this->check()) {
            $isFollowed = $this->userService->isFollowed($this->id(), $plan->user_id);
            $isThumbsuped = $this->userService->isThumbsuped($this->id(), $id, Plan::class);
        }
        return $this->successData(compact('plan', 'isFollowed', 'isThumbsuped'));

    }


    public function store(PlanRequest $request, Plan $plan)
    {


        $data = $request->filldata();
        $plan->fill($data)->save();
        return $this->successData(compact('plan'));
    }

    /**挑战编辑
     * @param Request $request
     * @param $id
     * @return JsonResponse
     */
    public function edit(Request $request, $id)
    {

        $plan = Plan::query()
            ->where('id', $id)
            ->firstOrFail();
        return $this->successData(compact("plan"));

    }

    /**
     * @param Request $request
     * @param $id
     * @return JsonResponse
     */
    public function delete(Request $request, $id)
    {
        $plan = Plan::query()->where('id', $id)->firstOrFail();

        if ($plan->fragments()->exists()) {
            return $this->error(__('已有动态不能删除'));
        }
        DB::transaction(function () use ($id, $plan) {
            $plan->delete();
            $plan->users()->detach();
        });

        return $this->success(__('删除成功'));
    }


    /**
     * 更新活动
     * @param PlanRequest $request
     * @param $id
     * @return JsonResponse
     */
    public function update(PlanRequest $request, $id)
    {
        $data = $request->filldata();
        $plan = Plan::query()->where('id', $id)->firstOrFail();
        $plan->fill($data)->save();

        return $this->success(__('更新活动成功'));

    }

    /**
     * 挑战下的碎片详情
     * @param Request $request
     * @param $id
     * @return JsonResponse
     */
    public function fragment(Request $request, $id)
    {

        $size = $request->input('size', 10);
        $fragments = PlanFragment::query()
            ->with(['images', 'videos'])
            ->where('plan_id', $id)
            ->orderByDesc('created_at')
            ->paginate($size);
        //发布的人
        $users = User::query()
            ->whereIn('id', array_column($fragments->items(), 'user_id'))
            ->select(['id', 'nick_name', 'avatar', 'mobile'])
            ->get()
            ->keyBy('id');
        return $this->successData(compact('fragments', 'users'));

    }

    function  punch(Request $request,$planId)
    {
        $date = $request->input('date');
        // 如果没有传递日期，则使用当前日期
        $date = $date ? Carbon::parse($date) : Carbon::now();

        // 获取传递日期所在月份的开始和结束时间
        $startOfMonth = $date->startOfMonth()->toDateTimeLocalString();
        $endOfMonth = $date->endOfMonth()->toDateTimeLocalString();

        $dates = PlanFragment::query()
            ->where('plan_id', $planId)
            ->whereBetween('created_at', [$startOfMonth, $endOfMonth])
            ->get()
            ->pluck('created_at');

// 如果需要将日期字符串格式化为特定格式的字符串
        $dates = $dates->map(function($date) {
            // 格式化为特定格式，例如 'Y-m-d'
            return date('Y-m-d', strtotime($date));
        });
        $dates = $dates->unique();
        return $this->successData(compact('dates'));
    }




}
